package com.jdk.demo.io.服务单和客户端互传对象以及io流顺序问题;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	public static void main(String[] args) throws Exception {
		ServerSocket serverSocket = new ServerSocket(8888);
		Socket socket = serverSocket.accept();
		InputStream inputStream = socket.getInputStream();
		OutputStream outputStream = socket.getOutputStream();

		ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);

		for (int i = 0; i < 5; i++) {
			UserInfo userInfo = (UserInfo)objectInputStream.readObject();
			System.out.println("在服务端打印，第" + i + "次：" + userInfo);

			UserInfo userInfo2 = new UserInfo(i + 1, "serverUsername" + i, "serverPassword" + i);
			objectOutputStream.writeObject(userInfo2);
		}

		objectInputStream.close();
		objectOutputStream.close();
		outputStream.close();
		inputStream.close();
		socket.close();
		serverSocket.close();
	}

}
